package com.lord.label.utils;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.lord.label.common.Collect;
import com.lord.label.enums.ResultEnum;
import com.lord.label.exception.LabelException;

/**
 * 
 * @Description: 采集工具类
 * @author Lord
 * @date 2017年5月22日 下午8:00:22
 */
public class JsoupUtil {
	
	private static final String ID = "id";
	
	private static final String CLASS = "class";
	
	private static final String NAME = "name";
	
	private static final String STYLE = "style";
	
	/**
	 * 
	 * @Title: JsoupUnit
	 * @Description: 根据url返回请求结果
	 * @param url 目标地址
	 * @return
	 * @author Lord
	 * @date 2017年5月22日 下午8:00:38
	 */
	public static Collect JsoupUnit(String url) {
		try {
			Connection.Response response = Jsoup.connect(url)
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
					.timeout(5000).execute();
			if(response.statusCode() == 200){
				
				Collect collect = new Collect();
				collect.setStatusCode(response.statusCode());
				collect.setStatusMessage(response.statusMessage());
				collect.setCharset(response.charset());
				collect.setContentType(response.contentType());
				collect.setTitle(response.parse().title());
				collect.setParse(response.parse());
				collect.setUrl(response.url());
				
				return collect;
			}
		} catch (IOException e) {
			throw new LabelException(ResultEnum.COLLECTION_ERROR);
		}
		return null;
	}
	
	/**
	 * 
	 * @Title: getWebSite
	 * @Description: 根据URL返回网站标识
	 * @param url
	 * @return
	 * @author Lord
	 * @date 2017年5月25日 下午1:49:10
	 */
	public static String getWebSite(String url){
		if(url.indexOf("csdn") > -1){
			return "csdn";
		}
		if(url.indexOf("cnblogs") > -1){
			return "cnblogs";
		}
		if(url.indexOf("iteye") > -1){
			return "iteye";
		}
		if(url.indexOf("sina") > -1){
			return "sina";	
		}
		if(url.indexOf("oschina") > -1){
			return "oschina";	
		}
		if(url.indexOf("aliyun") > -1){
			return "aliyun";	
		}
		if(url.indexOf("jb51") > -1){
			return "jb51";	
		}
		return null;
	}

	/**
	 * 
	 * @Title: getContent
	 * @Description: 根据html属性获取文章正文
	 * @param doc	html元素
	 * @param attribute	正文标签属性
	 * @return
	 * @author Lord 
	 * @date 2017年5月25日 下午2:29:19
	 */
	public static String getContent(Document doc, String attribute){
		Elements elements = null;
		if(StringUtils.isNotEmpty(attribute)&&StringUtils.isNotBlank(attribute)){
			elements = doc.getElementsByAttributeValue(CLASS,attribute);
		}
		if(elements.size() <= 0){
			elements = doc.getElementsByAttributeValue(ID,attribute);
		}
		if(elements.size() <= 0){
			elements = doc.getElementsByAttributeValue(NAME,attribute);
		}
		if(elements.size() <= 0){
			elements = doc.getElementsByAttributeValue(STYLE,attribute);
		}
		if(elements.size() <= 0){
			elements = doc.getElementsByTag(attribute);
		}
		return elements.html();
	}
}
